package cn.bugstack.mybatis.executor;

import cn.bugstack.mybatis.cache.CacheKey;
import cn.bugstack.mybatis.mapping.BoundSql;
import cn.bugstack.mybatis.mapping.MappedStatement;
import cn.bugstack.mybatis.session.ResultHandler;
import cn.bugstack.mybatis.session.RowBounds;
import cn.bugstack.mybatis.transaction.Transaction;

import java.sql.SQLException;
import java.util.List;

/**
 * SqlSession 每个方法的执行都需要经历 datasource、preparedStatement、resultSet等一系列操作
 * 将这部分操作解耦到 Executor
 * @author xing
 */
public interface Executor {

    ResultHandler NO_RESULT_HANDLER = null;

    <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws Exception;

    CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);

    int update(MappedStatement ms, Object parameter) throws Exception;

    void commit(boolean b) throws SQLException;

    Transaction getTransaction();

    /**
     * 根据key走缓存查询
     * @param ms
     * @param parameter
     * @param rowBounds
     * @param resultHandler
     * @param key
     * @param boundSql
     * @return
     * @param <E>
     * @throws Exception
     */
    <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws Exception;

    // 清理Session缓存
    void clearLocalCache();


    void rollback(boolean required) throws SQLException;

    void close(boolean forceRollback);

    void setExecutorWrapper(CachingExecutor cachingExecutor);
}
